﻿#ifndef JZNET_SERVER_H_
#define JZNET_SERVER_H_

#include <QObject>
#include <QMap>
#include "JZRpcDataManager.h"

class QTcpSocket;
class QTcpServer;

class JZRpcServer : public QObject {
	Q_OBJECT

public:
	JZRpcServer(QObject * parent = NULL);
	~JZRpcServer();
		
	bool startServer(int port);
	void stopServer();
	bool isOpen();	

	bool hasConnect(int net_id);
    void closeConnect(int netId);	

    bool sendPack(int netId,const QJsonObject &pack);
    bool sendPackExclude(int netId,const QJsonObject &pack);
    bool sendPackToAll(const QJsonObject &pack);

signals:		
	void sigNewConnect(int netId);	
	void sigDisConnect(int netId);	
	void sigNetPackRecv(int netId,QJsonObject ptr);

private slots:
	void onNewConnect();
	void onReadyRead();
	void onDisconnected();

private:	
	void closeSocket(int netId);
	
	QTcpServer *tcpServer;					
	QMap<int, QTcpSocket*> m_tcpClients;

	int m_netId;
	JZRpcDataManager m_dataManager;
};

#endif
